//
//  HYLViewController.m
//  qq通信
//
//  Created by hyl on 16/6/2.
//  Copyright © 2016年 hyl. All rights reserved.
//

#import "HYLViewController.h"
#import "HYLChat.h"
#import "HYLChatCell.h"
@interface HYLViewController () <UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic) NSArray *messages;
@property (weak, nonatomic) IBOutlet UITextField *textView;
@end

@implementation HYLViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.dataSource=self;
    self.tableView.delegate=self;
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    //
    self.textView.leftView=[[UIView alloc]initWithFrame:CGRectMake(0, 5, 8, 8)];
    self.textView.leftViewMode=UITextFieldViewModeAlways;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KeyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
    //self.view.transform=CGAffineTransformMakeTranslation(0, -100);
}
#pragma mark - KeyboardWillChange
-(void) KeyboardWillChange:(NSNotification *) note{
    NSLog(@"%@",note.userInfo);
    CGRect rect=[note.userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
    CGFloat transformHeight=[UIScreen mainScreen].bounds.size.height-rect.origin.y;
    CGFloat time=[note.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] doubleValue];
    NSLog(@"%f",transformHeight);
    NSLog(@"%f  ----",[UIScreen mainScreen].bounds.size.height);
     NSLog(@"%@",self.view);
    [UIView animateWithDuration:time
                     animations:^{
                         self.view.transform=CGAffineTransformMakeTranslation(0, -transformHeight);
                     }];
    NSLog(@"%@",self.view);
    NSLog(@"%f",self.view.bounds.origin.y);
}
#pragma mark - setter/getter
-(NSArray *) messages{
    if (_messages==nil) {
        NSString *path=[[NSBundle mainBundle]pathForResource:@"messages.plist" ofType:nil];
        NSArray *ArrMessage=[NSArray arrayWithContentsOfFile:path];
        NSMutableArray *ArrM=[NSMutableArray array];
        for (NSDictionary *dic in ArrMessage) {
            [ArrM addObject:[HYLChat chatWithDic:dic]];
        }
        _messages=ArrM;
    }
    return  _messages;
}

#pragma mark - UITableViewDataSource
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.messages.count;
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HYLChatCell *cell=[HYLChatCell chatCell:tableView];
    cell.chat=self.messages[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate
-(CGFloat) tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [self.messages[indexPath.row] cellHight];
}

#pragma mark - ScrollViewDelegate
-(void) scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.view endEditing:true];
}
@end
